package sinoservices.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import sinoservices.demo.entity.CommonResult;
import sinoservices.demo.entity.CustomerInfo;
import sinoservices.demo.service.CustomerInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：乔帅阳
 * @date ：Created in 2021/12/11 22:33
 *         此处异常无做处理、数据库除必要字段外，其他辅助字段不加
 * @modified By：
 */
@RestController
@Slf4j
public class CustormerInfoController {

    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping(value = "/Custormer/saveCustormer")
    public CommonResult saveCustormer(@RequestBody CustomerInfo customerInfo){
        int result = customerInfoService.saveCustomer(customerInfo);
        log.info("*****插入结果："+result);
        if (result>0){  //成功
            return new CommonResult(200,"插入数据库成功");
        }else {
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @RequestMapping(value = "/delCustormer/{id}",method = RequestMethod.GET)
    public CommonResult deleteCustormer(@PathVariable("id") int id){
        int result = customerInfoService.deleteCustomerById(id);
        if (result>0){
            return new CommonResult(200,"删除成功");
        }else {
            return new CommonResult(444,"删除记录失败，请稍后重试："+id,null);
        }
    }

    @RequestMapping(value = "/updateCustomer",method = RequestMethod.POST)
    public String update(@RequestBody CustomerInfo customerInfo) {
        int result = customerInfoService.updateCustomer(customerInfo);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @RequestMapping(value = "/getCustomerr/{id}",method = RequestMethod.GET)
    public String getCustomerInfo(@PathVariable int id){
        return customerInfoService.getCustomerInfo(id).toString();
    }

        @RequestMapping("/selectAllCustomer")
    public List<CustomerInfo> ListCustomer() {
        return customerInfoService.selectAllCustomer();
    }

    /**
     * 每次十年最小值和
     * @return
     */
    @RequestMapping("/querySum")
    public Integer querySum() {
        return customerInfoService.selectMinSum();

    }
}
